package com.hkts.naturalgas.base.service.serviceimpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hkts.naturalgas.base.entity.RoastingtwoRsoneRsjone;
import com.hkts.naturalgas.base.entity.RoastingtwoRsoneRsjtwo;
import com.hkts.naturalgas.base.entity.RoastingtwoRsoneRsjtwo;
import com.hkts.naturalgas.base.mapper.RoastingtwoRsoneRsjtwoMapper;
import com.hkts.naturalgas.base.service.IRoastingtwoRsoneRsjtwoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hkts.naturalgas.utils.CurveUtils;
import com.hkts.naturalgas.utils.DataHandlerUtils;
import com.hkts.naturalgas.utils.SnowFlakeUtil;
import com.hkts.naturalgas.utils.resultMapUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.apache.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.text.ParseException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yck
 * @since 2021-06-25
 */
@Service
@Transactional
public class RoastingtwoRsoneRsjtwoServiceImpl extends ServiceImpl<RoastingtwoRsoneRsjtwoMapper, RoastingtwoRsoneRsjtwo> implements IRoastingtwoRsoneRsjtwoService {


    @Autowired
    private RoastingtwoRsoneRsjtwoMapper roastingtwoRsoneRsjtwoMapper;

    @Override
    public IPage<RoastingtwoRsoneRsjtwo> selectPage(Page<RoastingtwoRsoneRsjtwo> page, RoastingtwoRsoneRsjtwo roastingtwoRsoneRsjtwo){
            QueryWrapper<RoastingtwoRsoneRsjtwo> query=Wrappers.query();
            QueryWrapper<RoastingtwoRsoneRsjtwo> entityWrapper=getEntityWrapper(query, roastingtwoRsoneRsjtwo);
            return roastingtwoRsoneRsjtwoMapper.selectPage(page,entityWrapper);
    }


    @Override
    public List<RoastingtwoRsoneRsjtwo> selectList(RoastingtwoRsoneRsjtwo roastingtwoRsoneRsjtwo){
            QueryWrapper<RoastingtwoRsoneRsjtwo> query=Wrappers.query();
            QueryWrapper<RoastingtwoRsoneRsjtwo> entityWrapper=getEntityWrapper(query, roastingtwoRsoneRsjtwo);
            return roastingtwoRsoneRsjtwoMapper.selectList(entityWrapper);
    }

    /**
     *  公共查询条件
     * @param entityWrapper
     * @return
     */
    public QueryWrapper<RoastingtwoRsoneRsjtwo> getEntityWrapper(QueryWrapper<RoastingtwoRsoneRsjtwo> entityWrapper,RoastingtwoRsoneRsjtwo roastingtwoRsoneRsjtwo){
            //条件拼接
        
            return entityWrapper;
    }
    @Override
    public IPage<RoastingtwoRsoneRsjtwo> selectPageByTime(Page<RoastingtwoRsoneRsjtwo> page, String startDate, String endDate){
        QueryWrapper<RoastingtwoRsoneRsjtwo> query = Wrappers.query();
        query.lambda().apply(!"".equals(startDate),
                "time >= '" + startDate + "'")
                .apply(!"".equals(endDate),
                        "time <= '" + endDate + "'")
                .orderByDesc(RoastingtwoRsoneRsjtwo::getTime);
        IPage<RoastingtwoRsoneRsjtwo> hktsTailGasPage = roastingtwoRsoneRsjtwoMapper.selectPage(page, query);
        return hktsTailGasPage;
    }
    @Override
    public IPage<RoastingtwoRsoneRsjtwo> getBrokenLineGraph(Page<RoastingtwoRsoneRsjtwo> page,String startDate, String endDate){
        QueryWrapper<RoastingtwoRsoneRsjtwo> query = Wrappers.query();
        query.lambda().apply(!"".equals(startDate),
                "time >= ' " + startDate + " ' " )
                .apply(!"".equals(endDate),
                        "time <= ' " + endDate + " ' ")
                .orderByAsc(RoastingtwoRsoneRsjtwo::getTime);
        IPage roastingoneRsoneRsjonePage = roastingtwoRsoneRsjtwoMapper.selectPage(page,query);
        List<RoastingtwoRsoneRsjtwo> records = roastingoneRsoneRsjonePage.getRecords();
        List<Map<String,Object>> resultDate = records.stream().map(t -> assemblyCargoData(t)).collect(Collectors.toList());
        Map<String, List<Object>> stringListMap = resultMapUtils.resultData(resultDate);
        List<Object> list1 = resultMapUtils.getList(resultDate);
        List<Object> listName = resultMapUtils.getListName(resultDate);
        Map<String,Object> data = new HashMap<>();
        data.put("list",listName);
        data.put("records",stringListMap);
        data.put("times",list1);
        List<Object> resultList = new ArrayList<>();
        resultList.add(data);
        IPage iPage = roastingoneRsoneRsjonePage.setRecords(resultList);
        return iPage;
    }

    public Map<String,Object> assemblyCargoData(RoastingtwoRsoneRsjtwo vo){
        Map<String,Object> data = new LinkedHashMap<>();
        data.put("火道温度1(℃)",vo.getHdw1());
        data.put("火道温度2(℃)",vo.getHdw2());
        data.put("火道温度3(℃)",vo.getHdw3());
        data.put("火道温度4(℃)",vo.getHdw4());
        data.put("火道温度5(℃)",vo.getHdw5());
        data.put("火道温度6(℃)",vo.getHdw6());
        data.put("火道温度7(℃)",vo.getHdw7());
        data.put("火道温度8(℃)",vo.getHdw8());
        data.put("火道温度9(℃)",vo.getHdw9());
        data.put("火道温度10(℃)",vo.getHdw10());
        data.put("火道温度11(℃)",vo.getHdw11());
        data.put("火道温度12(℃)",vo.getHdw12());
        data.put("火道温度13(℃)",vo.getHdw13());
        data.put("火道温度14(℃)",vo.getHdw14());
        data.put("功率1",vo.getGl1());
        data.put("功率2",vo.getGl2());
        data.put("功率3",vo.getGl3());
        data.put("功率4",vo.getGl4());
        data.put("功率5",vo.getGl5());
        data.put("功率6",vo.getGl6());
        data.put("功率7",vo.getGl7());
        data.put("功率8",vo.getGl8());
        data.put("功率9",vo.getGl9());
        data.put("功率10",vo.getGl10());
        data.put("功率11",vo.getGl11());
        data.put("功率12",vo.getGl12());
        data.put("功率13",vo.getGl13());
        data.put("功率14",vo.getGl14());
        data.put("time",vo.getTime());
        return data;
    }

    @Override
    public void addHostoryData(String startDate, String endDate) throws ParseException {
        CurveUtils utils=new CurveUtils();
        utils.setStartDate(startDate);
        utils.getGrowthRate().add(8.75f);
        utils.getGrowthRate().add(10f);
        utils.getGrowthRate().add(10.5294f);
        utils.getGrowthRate().add(0.9675f);
        utils.getGrowthRate().add(0f);
        //时间段
        utils.getTimeSlot().add(8);
        utils.getTimeSlot().add(7);
        utils.getTimeSlot().add(17);
        utils.getTimeSlot().add(32);
        utils.getTimeSlot().add(32);
        utils.setEndDate(endDate);
        utils.getData(780f);
        for(int i=0;i<utils.getDateList().size();i++){
            RoastingtwoRsoneRsjtwo one=new RoastingtwoRsoneRsjtwo(String.valueOf(SnowFlakeUtil.getFlowIdInstance().nextId()),
                    utils.getDateList().get(i),
                    DataHandlerUtils.getData(utils.getDataResult().get(i)),
                    DataHandlerUtils.getData(utils.getDataResult().get(i)),
                    DataHandlerUtils.getData(utils.getDataResult().get(i)),
                    DataHandlerUtils.getData(utils.getDataResult().get(i)),
                    DataHandlerUtils.getData(utils.getDataResult().get(i)),
                    DataHandlerUtils.getData(utils.getDataResult().get(i)),

                    DataHandlerUtils.getData(utils.getDataResult().get(i)),
                    DataHandlerUtils.getData(utils.getDataResult().get(i)),
                    DataHandlerUtils.getData(utils.getDataResult().get(i)),
                    DataHandlerUtils.getData(utils.getDataResult().get(i)),
                    DataHandlerUtils.getData(utils.getDataResult().get(i)),
                    DataHandlerUtils.getData(utils.getDataResult().get(i)),DataHandlerUtils.getData(utils.getDataResult().get(i)),DataHandlerUtils.getData(utils.getDataResult().get(i)),0f,0f,0f,0f,0f,0f,0f,0f,
                    0f,0f,0f,0f,0f,0f
            );
            roastingtwoRsoneRsjtwoMapper.insert(one);
        }
    }

    @Override
    public void handlerCurveData(String startDate, String endDate, Float mbt, String order) {
        QueryWrapper<RoastingtwoRsoneRsjtwo> query=Wrappers.query();
        query.le("time",endDate);
        query.ge("time",startDate);
        List<RoastingtwoRsoneRsjtwo> roastingonePyjones = roastingtwoRsoneRsjtwoMapper.selectList(query);
        List<Float> list=new ArrayList<Float>();
        if(order.equals("desc")){
            list=  DataHandlerUtils.getTemperatureCurve(mbt,roastingonePyjones.get(0).getHdw1(),roastingonePyjones.size(),order);
        }else{
            list=  DataHandlerUtils.getTemperatureCurve(mbt,roastingonePyjones.get(roastingonePyjones.size()-1).getHdw1(),roastingonePyjones.size(),order);
        }
        for(int i=0;i<roastingonePyjones.size();i++){
            roastingonePyjones.get(i).setHdw1( DataHandlerUtils.getData(list.get(i)));
            roastingonePyjones.get(i).setHdw2(DataHandlerUtils.getData(list.get(i)));
            roastingonePyjones.get(i).setHdw3( DataHandlerUtils.getData(list.get(i)));
            roastingonePyjones.get(i).setHdw4(DataHandlerUtils.getData(list.get(i)));
            roastingonePyjones.get(i).setHdw5( DataHandlerUtils.getData(list.get(i)));
            roastingonePyjones.get(i).setHdw6(DataHandlerUtils.getData(list.get(i)));
            roastingonePyjones.get(i).setHdw7( DataHandlerUtils.getData(list.get(i)));
            roastingonePyjones.get(i).setHdw8(DataHandlerUtils.getData(list.get(i)));
            roastingonePyjones.get(i).setHdw9( DataHandlerUtils.getData(list.get(i)));
            roastingonePyjones.get(i).setHdw10(DataHandlerUtils.getData(list.get(i)));
            roastingonePyjones.get(i).setHdw11( DataHandlerUtils.getData(list.get(i)));
            roastingonePyjones.get(i).setHdw12( DataHandlerUtils.getData(list.get(i)));
            roastingonePyjones.get(i).setHdw13( DataHandlerUtils.getData(list.get(i)));
            roastingonePyjones.get(i).setHdw14( DataHandlerUtils.getData(list.get(i)));
        }
        this.saveOrUpdateBatch(roastingonePyjones);
    }
}
